{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Interpolating Network Sets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Frequently a set of `Networks` is recorded while changing some other parameters; like temperature, voltage, current, etc. Once this set of data acquired, it is sometime usefull to estimate the behaviour of the network for parameter values that lie in between those that have been measured. For that purpose, interpolating a network from a set of network is possible. This example demonstrates how to do this using [NetworkSets](../../tutorials/NetworkSet.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "import skrf as rf\n",
    "\n",
    "rf.stylely()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Narda 3752 phase shifter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example, we are characterizing an old [narda phase shifter 3752](https://nardamiteq.com/docs/119-PHASESHIFTERS.PDF) at 1.5 GHz. \n",
    "![narda 3752 phase shifter](phase_shifter_measurements/Narda_3752.jpg) :\n",
    "\n",
    "In order to deduce the phase shift that one can obtain at this specific frequency, we have measured scattering parameters in the 1-2 GHz band at 19 positions of the phase knob (from 0 to 180). These measurements are loaded into a [NetworkSets](../../tutorials/NetworkSet.ipynb) object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Array containing the 19 phase shift indicator values\n",
    "indicators_mes = np.linspace(0, 180, num=19)  # from 0 to 180 per 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ntw_set = rf.NetworkSet.from_zip('phase_shifter_measurements/phase_shifter_measurements.zip')\n",
    "print('ntw_set contains', len(ntw_set), 'networks')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We extract from the network set the phase shift and $S_{11}$ at the specific frequency of 1.5 GHz:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = '1.5 GHz'\n",
    "phases_mes = np.squeeze([ntw[f].s21.s_deg for ntw in ntw_set])\n",
    "s11_mes = np.squeeze([ntw[f].s11.s_db for ntw in ntw_set])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We would like however to get the phase shift values for intermediate settings of this phase shifter. To that purpose, let's create a network from the interpolation of the measured networks. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# the indicator values for which we want to interpolate the network\n",
    "indicators = np.linspace(0, 180, num=181)  # every degrees for 0 to 180\n",
    "\n",
    "phases_interp = [ntw_set.interpolate_from_network(indicators_mes, phi)['1.5GHz'].s21.s_deg for phi in indicators]\n",
    "phases_interp = np.squeeze(phases_interp)\n",
    "\n",
    "s11_interp = [ntw_set.interpolate_from_network(\n",
    "    indicators_mes, phi, interp_kind='quadratic')['1.5GHz'].s11.s_db for phi in indicators]\n",
    "s11_interp = np.squeeze(s11_interp)\n",
    "\n",
    "print('We have interpolated the network for', len(phases_interp), 'points')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's plot everything"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, (ax1, ax2) = plt.subplots(2,1, figsize=(10,7), sharex=True)\n",
    "ax1.set_title('Narda Phase Shifter vs Indicator @ 1.5 GHz', fontsize=10)\n",
    "ax1.plot(indicators, s11_interp, label='Interpolated network')\n",
    "ax1.plot(indicators_mes, s11_mes, '.', ms=10, label='Measurements')\n",
    "ax1.legend()\n",
    "ax1.set_ylabel(r'$S_{11}$ [dB]')\n",
    "\n",
    "ax2.plot(indicators, phases_interp, label='Interpolated network')\n",
    "ax2.plot(indicators_mes, phases_mes, '.', ms=10, label='Measurements')\n",
    "ax2.set_xlabel('Phase shifter indicator')\n",
    "ax2.set_ylabel('$S_{21}$ [deg]')\n",
    "\n",
    "ax2.fill_between(indicators, phases_mes[0], phases_mes[-1], alpha=0.2, color='r')\n",
    "ax2.text(40, 25, 'not available zone')\n",
    "ax2.legend()\n",
    "ax2.set_ylim(-200, 200)\n",
    "\n",
    "fig.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
